/*
 * Copyright (c) 2011 Jean-Marc Perronne
 * Ensisa, 12 rue des Freres Lumieres, F-68093 Mulhouse cedex
 * All rights reserved.
 *
 * The copyright of this software is owned by Werner Randelshofer. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jean-Marc Perronne. For details see accompanying license terms. 
 */

package fr.uha.ensisa.jmp.drone.ui.instrument.linear;

import java.awt.Color;
import java.awt.Component;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.BoundedRangeModel;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import fr.uha.ensisa.jmp.ui.utils.ColoredRange;
import fr.uha.ensisa.jmp.ui.utils.ColoredRanges;
import fr.uha.ensisa.jmp.ui.utils.PassiveSlider;


public class LinearIndicator extends JPanel
{
	private final static ColoredRanges DEFAULT_COLORED_RANGES;

	static
	{
		DEFAULT_COLORED_RANGES = new ColoredRanges();
		DEFAULT_COLORED_RANGES.addRange(new ColoredRange(0, 24, Color.RED));
		DEFAULT_COLORED_RANGES.addRange(new ColoredRange(25, 40, Color.ORANGE));
		DEFAULT_COLORED_RANGES.addRange(new ColoredRange(41, 50, Color.YELLOW));
		DEFAULT_COLORED_RANGES.addRange(new ColoredRange(51, 100, Color.GREEN));
	}

	private JLabel label;
	private PassiveSlider indicator;
	
	public LinearIndicator(String label, int orientation)
	{
		this(label, orientation, 0, 100);
	}
	
	public LinearIndicator(String label, int orientation, int min, int max)
	{
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
	
		this.label = new JLabel(label, JLabel.CENTER);
		this.indicator =  new PassiveSlider(orientation, min, max, 0);
		
		this.label.setAlignmentX(Component.CENTER_ALIGNMENT);
		this.indicator.setAlignmentX(Component.CENTER_ALIGNMENT);


		if (orientation == JSlider.HORIZONTAL) 
		{
			this.add(this.label);
			this.add(this.indicator);
		
		}
		else
		{
			this.add(this.indicator);
			this.add(this.label);
		}
		
		this.setDefaultproperties(min, max);
	}	
		
	protected void setDefaultproperties(int min, int max)
	{
		this.indicator.setMajorTickSpacing((max-min)/4);
		this.indicator.setPaintTicks(true);
		this.indicator.setPaintLabels(true);
		this.indicator.setPaintTrack(true);
		this.indicator.setPaintTicks(true);
		this.setColorProperties(DEFAULT_COLORED_RANGES);
	}
	
	public void setColorProperties(ColoredRanges coloredRanges)
	{
		Map<Integer, JLabel> labels = ((Hashtable<Integer, JLabel>)this.indicator.getLabelTable());
		if (labels == null) return;
		
		for(Entry<Integer, JLabel> e :labels.entrySet())
		{
			ColoredRange cr = coloredRanges.getRange(e.getKey().intValue());

			if (cr !=null) e.getValue().setForeground(cr.color);
			else e.getValue().setForeground(Color.BLACK);
		}
	}

	public BoundedRangeModel getModel()
	{
		return this.indicator.getModel();
	}
	
	public void setMajorTickSpacing(int v)
	{
		this.indicator.setMajorTickSpacing(v);
	}

	public void setMinorTickSpacing(int v)
	{
		this.indicator.setMinorTickSpacing(v);
	}

	public void setPaintLabels(boolean v)
	{
		this.indicator.setPaintLabels(v);
	}

	public void setPaintTicks(boolean v)
	{
		this.indicator.setPaintTicks(v);
	}

	public void setPaintTrack(boolean v)
	{
		this.indicator.setPaintTrack(v);
	}

	public void labelColor(Color c)
	{
		this.label.setForeground(c);
	}
	
	public void setValue(int value)
	{
		this.indicator.setValue(value);
	}
	
	public void setMaximum(int value)
	{
		this.indicator.setMaximum(value);
	}
	
	public void setMinimum(int value)
	{
		this.indicator.setMinimum(value);
	}
}
